if ( CKEDITOR.env.ie && CKEDITOR.env.version < 9 )
    CKEDITOR.tools.enableHtml5Elements( document );

// The trick to keep the editor in the sample quite small
// unless user specified own height.
CKEDITOR.config.height = 600;
CKEDITOR.config.toolbarCanCollapse = true;
CKEDITOR.config.width = 'auto';

var ucEditCommentInit = ( function() {
    var wysiwygareaAvailable = isWysiwygareaAvailable(),
        isBBCodeBuiltIn = !!CKEDITOR.plugins.get( 'bbcode' );

    return function() {
        var editorElement = CKEDITOR.document.getById( 'editor' );

        // :(((
        if ( isBBCodeBuiltIn ) {
            editorElement.setHtml(
                'Hello world!\n\n' +
                'I\'m an instance of [url=https://ckeditor.com]CKEditor[/url].'
            );
        }

        // Depending on the wysiwygarea plugin availability initialize classic or inline editor.
        if ( wysiwygareaAvailable ) {
            CKEDITOR.replace( 'editor' );
        } else {
            editorElement.setAttribute( 'contenteditable', 'true' );
            CKEDITOR.inline( 'editor' );

            // TODO we can consider displaying some info box that
            // without wysiwygarea the classic editor may not work.
        }
    };

    function isWysiwygareaAvailable() {
        // If in development mode, then the wysiwygarea must be available.
        // Split REV into two strings so builder does not replace it :D.
        if ( CKEDITOR.revision == ( '%RE' + 'V%' ) ) {
            return true;
        }

        return !!CKEDITOR.plugins.get( 'wysiwygarea' );
    }
} )();


function ucContinueBookCommentOnSaveClick(target) {
    var id = $('#tag-save-submit').attr('value').trim();
    var title = $('#bookcomment-title').val();
    var data = CKEDITOR.instances.editor.getData();

    if (data == '') {
        showDialog("内容是空的");
    }
    else {
        $.ajax(
            {
                type: 'post',
                async: true,
                data: {'title': title, 'data': data, "id" : id},
                url: document.location.origin + '/api/savebookcomment',
                dataType:'text',
                success: function(data) {
                    var json = JSON.parse(data);

                    if (json.result == 200) {
                        if (id.trim() == '') {
                            $('#tag-save-submit').attr('value', json.id.trim());
                        }
                        var saveInfo = "最后保存时间：" + json.savetime;
                        $('#bookcomment-status').text(saveInfo);
                        showAutoDialog(3000, 'success', "已经保存");
                    }
                    else {
                        showDialog(json.description);
                    }
                },
                error: function () {
                    console.log("Ajax 发生错误！");
                }
            }
        );
    }
}

function ucContinueBookCommentOnSubmitClick(target) {
    var id = $('#tag-save-submit').attr('value').trim();
    var data = CKEDITOR.instances.editor.getData();
    var title = $('#bookcomment-title').val();

    if (data == '' || title.trim() == '') {
        showDialog("内容或者题目是空的");
    }
    else {

        $.ajax(
            {
                type: 'post',
                async: true,
                data: {'title': title, 'data': data, "id" : id},
                url: document.location.origin + '/api/submitbookcomment',
                dataType:'text',
                success: function(data) {
                    var json = JSON.parse(data);

                    if (json.result == 200) {
                        var path = "/renderbookcomment/" + URLencode(json.id);
                        showDialogToJumpToPage("发布成功，跳往发布文章页面", path, "确认");
                    }
                    else {
                        showDialog(json.description);
                    }
                },
                error: function () {
                    console.log("Ajax 发生错误！");
                }
            }
        );
    }
}

$(document).ready(function() {
    ucEditCommentInit();
});